/*
 * @author: wizardc
 */

using UnityEngine;
using UnityEngine.Rendering.Universal;

namespace Dou.URP
{
    /// <summary>
    /// URP 摄像机工具类
    /// </summary>
    public static class URPCameraUtil
    {
        /// <summary>
        /// 添加一个 Overlay 相机
        /// </summary>
        public static void AddOverlay(Camera baseCamera, Camera overlayCamera, int cameraDepth = 0)
        {
            var data = baseCamera.GetUniversalAdditionalCameraData();
            if (data == null)
            {
                return;
            }
            var overlayData = overlayCamera.GetUniversalAdditionalCameraData();
            if (overlayData.renderType != CameraRenderType.Overlay)
            {
                return;
            }
            if (data.cameraStack.Contains(overlayCamera))
            {
                return;
            }
            if (cameraDepth != 0)
            {
                overlayCamera.depth = cameraDepth;
            }
            data.cameraStack.Add(overlayCamera);
            data.cameraStack.Sort((a, b) => a.depth.CompareTo(b.depth));
        }

        /// <summary>
        /// 移除一个 Overlay 相机
        /// </summary>
        public static void RemoveOverlay(Camera baseCamera, Camera overlay)
        {
            var data = baseCamera.GetUniversalAdditionalCameraData();
            if (data == null)
            {
                return;
            }
            var overlayData = overlay.GetUniversalAdditionalCameraData();
            if (overlayData.renderType != CameraRenderType.Overlay)
            {
                return;
            }
            if (!data.cameraStack.Contains(overlay))
            {
                return;
            }
            data.cameraStack.Remove(overlay);
        }
    }
}
